package com.jsmy.marketing.activity;

import android.os.Build;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.ButtonBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsmy.marketing.R;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.base.BaseActivity;
import com.jsmy.marketing.bean.UserInfo;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.manager.UserManager;
import com.jsmy.marketing.utils.NetWork;
import com.jsmy.marketing.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class UserActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.buttonBarLayout)
    ButtonBarLayout buttonBarLayout;
    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    @BindView(R.id.img_bg)
    ImageView imgBg;
    @BindView(R.id.img_avatar)
    ImageView imgAvatar;

    boolean isAvatar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.toolbar_avatar)
    ImageView toolbarAvatar;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.m_webview)
    WebView mWebview;
    @BindView(R.id.tv_sex)
    TextView mTvSex;

    String sharePt = "";
    String shareId = "";

    @Override
    protected int resId() {
        return R.layout.activity_user;
    }

    @Override
    protected void initData() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        }
        appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
                if (i == 0) {
                    buttonBarLayout.setVisibility(View.GONE);
                } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
                    buttonBarLayout.setVisibility(View.VISIBLE);
                } else {
                    buttonBarLayout.setVisibility(View.GONE);
                }
            }
        });
        NetWork.setImageGlideCircle(this, UserManager.getInstance().getIco(), imgAvatar);
        NetWork.setImageGlideCircle(this, UserManager.getInstance().getIco(), toolbarAvatar);
        tvName.setText(UserManager.getInstance().getUsername());
        title.setText(UserManager.getInstance().getUsername());
        tvContent.setText(UserManager.getInstance().getTitle());

        mWebview.setVerticalScrollbarOverlay(true); //指定的垂直滚动条有叠加样式
        WebSettings settings = mWebview.getSettings();
        settings.setUseWideViewPort(true);//设定支持viewport
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);//设定支持缩放
        settings.setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebview.clearHistory();
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                return super.shouldOverrideUrlLoading(view, request);
            }
        });

//        mWebview.loadUrl(API.APP_SHARE + SPF.getString(MyApp.getMyApp(), SPF.SP_ZH, MyApp.getMyApp().bean.getUsername()));
        requestData();
    }

    public void requestData() {
        Map<String, Object> map = new HashMap();
        map.put("lsid", UserManager.getInstance().getId());
        mPresenter.loadData(RetrofitManager.getInstance().getlsinfo(map), Link.APP_getlsinfo);
    }

    @Override
    public void refreshData(Object object, String api_id) {
        switch (api_id) {
            case Link.APP_getlsinfo:
                UserInfo baseBean = (UserInfo) object;
                if (baseBean.getCode().equals("Y")) {

                    showUI(baseBean);
                } else {
                    ToastUtil.showShort(this, baseBean.getMsg());
                }
                break;
        }
    }

    private void showUI(UserInfo baseBean) {
        NetWork.setImageGlideCircle(this, baseBean.getData().getTx(), imgAvatar);
        NetWork.setImageGlideCircle(this, baseBean.getData().getTx(), toolbarAvatar);
        tvName.setText(baseBean.getData().getZwm());
        title.setText(baseBean.getData().getZwm());
        tvContent.setText(baseBean.getData().getDh());
        mTvSex.setText("性别："+baseBean.getData().getXb());
    }
}
